{% extends 'my_admin/table_index.html' %}
{% load tags %}


{% block continer %}
    <div class="row">
    {% include 'include/sidebar.html' %}
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3
                        class="panel-title">{% render_model_name admin_class %}
                    {% if not admin_class.readonly_table %}
                        <a href="{{ request.path_info }}/add/" class="pull-right">Add</a>
                    {% endif %}
                </h3>

            </div>
            <div class="panel-body">
                <form method="get">
                    <div class="row">
                        {% for current_condition in admin_class.list_filter %}
                            <div class="col-lg-2" style="text-align: center ">
                                <span>{{ current_condition.1 }}</span>
                                {% render_filter_ele current_condition admin_class conditions %}
                            </div>
                        {% endfor %}
                        <div class="col-lg-2">
                            <button type="submit" class="btn btn-success" style="margin-top: 19px">检索</button>
                        </div>
                    </div>
                    <div class="row" style="margin-top: 5px">
                        <div class="col-lg-4">
                            <input type="search" name="_q" class="form-control" value="{{ search_text }}"
                                   placeholder="search by {% render_placeholder admin_class.search_fields %}">
                        </div>
                        <div class="col-lg-2">
                            <button type="submit" class="btn btn-success">搜索</button>
                        </div>
                    </div>
                </form>
                <form id="doAction" method="post">
                {% csrf_token %}
                    <div class="row" style="margin-top: 5px">
                        <div class="col-md-3">
                            <select class="form-control" name="action">
                                <option value="">--------</option>
                                {% for action in admin_class.actions %}
                                    <option value="{{ action }}">{% render_action_name admin_class action %}</option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <button type="submit" class="btn btn-success">Go</button>
                        </div>
                    </div>

                </form>

                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th style="width: 15px">
                            <input type="checkbox" role="action_choice_all">
                        </th>
                        {% for column in admin_class.list_display %}
                            {% render_thead_column admin_class column orderby_key conditions page search_text  %}

                        {% endfor %}
                    </tr>
                    </thead>
                    <tfoot>
                    <tr>
                        <td colspan="2">总计{{ query_set.paginator.count }}条</td>
                    </tr>
                    </tfoot>
                    <tbody>
                    {% for  obj in query_set %}
                        <tr>
                            <td>
                                <input type="checkbox" role="action_choice_one" value="{{ obj.id }}">
                            </td>
                            {% render_obj_data request obj admin_class %}
                        </tr>
                    {% endfor %}
                    </tbody>

                </table>
                <div class="pagination">
                    {% if query_set.has_previous %}
                        {% render_page_previous query_set conditions privious_orderby_key search_text %}
                    {% endif %}

                    {% render_page_btns query_set conditions privious_orderby_key search_text %}

                    {% if query_set.has_next %}
                        {% render_page_next query_set conditions privious_orderby_key search_text %}
                    {% endif %}

                </div>

            </div>
        </div>
    </div>
    </div>

{% endblock %}


{% block custome_js %}
    <script>
        $(function () {
             $(':checkbox[role="action_choice_all"]').click(function () {
                 if($(this).prop('checked')){
                     $(':checkbox[role="action_choice_one"]').each(function(){
                         $(this).prop('checked',true)
                     })
                 }
                 else{
                     $(':checkbox[role="action_choice_one"]').each(function(){
                         $(this).prop('checked',false)
                     })
                 }
             });
             $(':checkbox[role="action_choice_one"]').click(function () {
                  if($(this).prop('checked')){
                      var flag = true;
                      $(':checkbox[role="action_choice_one"]').each(function () {
                          if(!$(this).prop('checked')){
                              flag = false;
                              return false;
                          }
                      });
                      if(flag){
                          $(':checkbox[role="action_choice_all"]').prop('checked', true);
                      }
                  }
                  else{
                      $(':checkbox[role="action_choice_all"]').prop('checked', false);

                  }
             });
             $('#doAction').submit(function () {
                 console.log($(this));
                 var selected_ids = [];    // 获取选中数据的id
                 $(':checkbox[role="action_choice_one"]:checked').each(function () {
                     selected_ids .push($(this).val());
                 });
                 var selected_action = $(this).find('select').val();
                 if(selected_ids.length == 0 ){
                     alert('没有选择任何数据');
                 }
                 else if(!selected_action){
                     alert('没有选择action');
                 }
                 else{
                     // 添加选中行
                     var select_ids_ele = document.createElement('input');
                     $(select_ids_ele).attr('name','selected_ids');
                     $(select_ids_ele).attr('type','hidden');
                     $(select_ids_ele).val(selected_ids);
                     $(this).append(select_ids_ele);

                      return true;
                 }
                 return false;
             });
        });
    </script>
{% endblock %}




